﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

using mvcForum.Core;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Interfaces.Data;
using mvcForum.Core.Specifications;

using mvcForum.Web.Extensions;
using mvcForum.Web.Interfaces;

namespace mvcForum.Web.ViewModels {

	public abstract class ForumViewModelBase {

		protected ForumViewModelBase() { }

		public Dictionary<String, String> Path { get; set; }

		private bool authenChecked = false;
		private bool authenticated = false;
		public bool Authenticated {
			get {
				if (!this.authenChecked) {
					this.authenticated = DependencyResolver.Current.GetService<IWebUserProvider>().Authenticated;
					this.authenChecked = true;
				}
				return this.authenticated;
			}
		}

		//protected DateTime LastRead(Topic topic) {
		//    // TODO: Remove this!!
		//    return topic.LastRead();
		//}

		//protected DateTime LastRead(Forum forum) {
		//    // TODO: Remove this!!
		//    return forum.LastRead();
		//}

		protected Boolean HasAccess(Forum forum) {
			return (forum.GetAccess() & AccessFlag.Read) == AccessFlag.Read;
		}

		protected Boolean HasAccess(Forum forum, AccessFlag flag) {
			return (forum.GetAccess() & flag) == flag;
		}

		private ForumUser currentUser = null;
		protected ForumUser CurrentUser {
			get {
				if (this.Authenticated && this.currentUser == null) {
					try {

						IWebUserProvider userProvider = DependencyResolver.Current.GetService<IWebUserProvider>();
						this.currentUser = userProvider.ActiveUser;
					}
					catch { /* TODO: Log this! */ }
				}
				return this.currentUser;
			}
		}

		/*protected IUserContext Context {
			get {
				if (context == null) {
					context = Factory.GetFactory().Resolve<IUserContext>();
				}
				return context;
			}
		}*/

		/*protected IUser CurrentUser {
			get {
				return this.Context.Current;
			}
		}*/

		/*public bool Authenticated {
			get {
				return (this.Context.Current != null && this.Context.Current.Id != -1);
			}
		}*/
	}
}